//
//  LCWPicCollection.swift
//  MyWB
//
//  Created by 李成万 on 16/6/30.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit

let itemSpace:CGFloat = 5
let itemWH = (SCREENW - itemSpace * 2 - Padding * 2)/3

class LCWPicCollection: UICollectionView {
    
    var pic_urls : [LCWPicInfo]?{
        didSet{
//            countLabel.text = "\(pic_urls?.count ?? 0)"
            
            let size = getSelfSize(pic_urls?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            reloadData()
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
        backgroundColor = UIColor.clearColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    private lazy var countLabel:UILabel = UILabel(messageColor: UIColor.redColor(), textFont: 20)
    
    func setupUI(){
        
        registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        dataSource = self
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumInteritemSpacing = itemSpace
        layout.minimumLineSpacing = itemSpace
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
//        addSubview(countLabel)
//        
//        
//        countLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self)
//        }
     
    }
    
    func getSelfSize(count:Int) -> CGSize{
        let lie = count == 4 ? 2 : (count >= 3 ? 3 : count)
        //count == 1 ? 1 : (count >= 5 ? 3 : 2)
        let hang = count == 4 ? 2 : ((count - 1) / 3 + 1)
        
        let w = CGFloat(lie) * itemWH + CGFloat(lie - 1) * itemSpace
        let h = CGFloat(hang) * itemWH + CGFloat(hang - 1) * itemSpace
       
        return CGSizeMake(w, h)
    }

}

extension LCWPicCollection : UICollectionViewDataSource{
    
    func creatImageView() -> UIImageView{
        let imageView : UIImageView = UIImageView()
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        imageView.userInteractionEnabled = true
        
        return imageView
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        
        let picInfo : LCWPicInfo = pic_urls![indexPath.item]
        
        let imageView = creatImageView()
        imageView.sd_setImageWithURL(NSURL(string: picInfo.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        
        cell.backgroundView = imageView
        //        cell.backgroundColor = randomColor()
        
        return cell
        
    }
}

class LCWPicCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
